package ru.braim.weather_app.controllers;

import jakarta.annotation.Nullable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ru.braim.weather_app.dto.weather_forecast.WeatherForecastRequestDTO;
import ru.braim.weather_app.dto.weather_forecast.WeatherForecastResponseDTO;
import ru.braim.weather_app.responses.weather_forecast.WeatherForecastResponse;
import ru.braim.weather_app.services.CookieService;
import ru.braim.weather_app.services.WeatherForecastService;

@RestController
@RequestMapping("/region/weather/forecast")
public class WeatherForecastController {

    private final WeatherForecastService weatherForecastService;
    private final CookieService cookieService;

    public WeatherForecastController(WeatherForecastService weatherForecastService, CookieService cookieService) {
        this.weatherForecastService = weatherForecastService;
        this.cookieService = cookieService;
    }

    @PostMapping("/")
    public ResponseEntity<WeatherForecastResponseDTO> createWeatherForecast
            (@RequestBody WeatherForecastRequestDTO weatherForecastRequestDTO,
             @CookieValue("id") @Nullable String cookie) {

        if (!cookieService.checkAuthorizeCookie(cookie)) {
            return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
        }

        WeatherForecastResponse weatherForecastResponse =
                weatherForecastService.createWeatherForecast(weatherForecastRequestDTO);

        return new ResponseEntity<>(weatherForecastResponse.getWeatherForecastResponseDTO(),
                weatherForecastResponse.getHttpStatus());
    }

    @GetMapping("/{forecastId}")
    public ResponseEntity<WeatherForecastResponseDTO> getWeatherForecastById(@PathVariable String forecastId,
                                                                             @CookieValue("id") @Nullable String cookie)
    {
        try {
            if (!cookieService.checkAuthorizeCookie(cookie)) {
                return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
            }

            WeatherForecastResponse weatherForecastResponse = weatherForecastService
                    .getWeatherForecastById(Long.parseLong(forecastId));

            return new ResponseEntity<>(weatherForecastResponse.getWeatherForecastResponseDTO()
                    , weatherForecastResponse.getHttpStatus());

        } catch (NumberFormatException e) {
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }

    }

    @PutMapping("/{forecastId}")
    public ResponseEntity<WeatherForecastResponseDTO> updateWeatherForecastById(@PathVariable String forecastId,
                                                                             @RequestBody WeatherForecastRequestDTO
                                                                                     weatherForecastRequestDTO,
                                                                             @CookieValue("id") @Nullable String cookie)
    {
        try {
            if (!cookieService.checkAuthorizeCookie(cookie)) {
                return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
            }

            WeatherForecastResponse weatherForecastResponse = weatherForecastService
                    .updateWeatherForecastById(Long.parseLong(forecastId), weatherForecastRequestDTO);

            return new ResponseEntity<>(weatherForecastResponse.getWeatherForecastResponseDTO()
                    , weatherForecastResponse.getHttpStatus());

        } catch (NumberFormatException e) {
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }

    }

    @DeleteMapping("/{forecastId}")
    public ResponseEntity<WeatherForecastResponseDTO> deleteWeatherForecastById(@PathVariable String forecastId,
                                                                                @CookieValue("id") @Nullable String cookie)
    {
        try {
            if (!cookieService.checkAuthorizeCookie(cookie)) {
                return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
            }

            WeatherForecastResponse weatherForecastResponse = weatherForecastService
                    .deleteWeatherForecastById(Long.parseLong(forecastId));

            return new ResponseEntity<>(weatherForecastResponse.getWeatherForecastResponseDTO()
                    , weatherForecastResponse.getHttpStatus());

        } catch (NumberFormatException e) {
            return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
        }

    }

}
